package generation;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class TabIdent {
	
	private HashMap<String, Ident> globaux;
	private HashMap<String, Ident> locaux;
	
	public TabIdent(int taille){
		globaux = new HashMap<String, Ident>(taille);
		locaux = new HashMap<String, Ident>(taille);
		globaux.put("_N/A", new IdConst("NON_DEFINI","nan"));
		locaux.put("_N/A", new IdConst("NON_DEFINI","nan"));
	}
	
	public TabIdent(){
		globaux = new HashMap<String, Ident>();
		locaux = new HashMap<String, Ident>();
		globaux.put("_N/A", new IdConst("NON_DEFINI","nan"));
		locaux.put("_N/A", new IdConst("NON_DEFINI","nan"));
	}
	
	public Ident chercheIdent(String clef){
		if(globaux.containsKey(clef)){return globaux.get(clef);}
		else if (locaux.containsKey(clef)){return locaux.get(clef);}
		else {return new IdConst("NON_DEFINI","nan");}
	}
	
	
	public boolean existeIdent(String clef){
		return (globaux.containsKey(clef) || locaux.containsKey(clef));
	}
	
	public void rangeIdLoc(String clef, Ident id){
		locaux.put(clef, id);
	}
	
	public void rangeIdGlob(String clef, Ident id){
		globaux.put(clef, id);
	}
	
	public void clearLocaux(){
		locaux.clear();
		locaux.put("_N/A", new IdConst("NON_DEFINI","nan"));
		Declaration.resetCpt();
		IdVar.resetOffset();
		Fonction.resetOffset();
	}

	public void decalOffset() {
		Set cles = locaux.keySet();
		Iterator it = cles.iterator();
		while (it.hasNext()){
		   String cle = (String) it.next();
		   Ident valeur = (Ident) locaux.get(cle);
		   if(!valeur.isConstante()){
			   ((IdVar)valeur).decalOffset();
		   }
		}

		
	}
	
}
